package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.basic.BasicSliderUI;
import java.util.List;

/*
* 游记资源控制器
* */
@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService travelCommendService;

    @PostMapping
    public JsonResult savaOrUpdate(Travel travel){
        travelService.savaOrUpdate(travel);
        return new JsonResult();
    }

    @PutMapping("/{id}")
    public JsonResult update(Travel travel){
        travelService.savaOrUpdate(travel);
        return new JsonResult();
    }
    // 根据游记的id去查询对应的游记信息
    @GetMapping("/{id}")
    public Travel input(@PathVariable Long id){
        Travel result = travelService.get(id);
        return result;
    }
    @GetMapping
    public PageInfo travelList(TravelQueryObject queryObject){
        // 查询所有的游记 -> 已经发布的而且是公开的游记
        queryObject.setState(Travel.STATE_RELEASE); // 已经发布
        queryObject.setIsPublic(true); // 公开
        PageInfo<Travel> pageInfo = travelService.query(queryObject);
        return pageInfo;
    }
    @GetMapping("/commends")
    public PageInfo listCommends(TravelCommendQueryObject queryObject){
        /* 查询的结果需要按推荐的时间降序排列*/
        queryObject.setOrderBy("t.schedule desc");
        PageInfo<TravelCommend> pageInfo = travelCommendService.queryForApp(queryObject);
        return pageInfo;
    }

}
